import java.util.Arrays;

public class Demo1_1 {
    //轮转数组
    //给定一个整数数组 nums，将数组中的元素向右轮转 k个位置，其中 k是非负数。
    //方法一：暴力解法
    public static void rotate(int[] nums, int k) {
        int startNum = 0, endNum = 0;
        for (int i = 0; i < k; i++) {
            endNum = nums[nums.length - 1];
            for (int j = 0; j < nums.length; j++) {
                startNum = nums[j];
                nums[j] = endNum;
                endNum = startNum;
            }
        }
        System.out.println(Arrays.toString(nums));
    }

    //方法二：反转法
    public static void reverse(int[] nums, int left, int right) {
        while (left < right) {
            int temp = nums[left];
            nums[left] = nums[right];
            nums[right] = temp;
            left++;
            right--;
        }
    }

    public static void rotate1(int[] nums, int k) {
        k %= nums.length;
        reverse(nums, 0, nums.length - 1);
        reverse(nums, 0, k - 1);
        reverse(nums, k, nums.length - 1);
        System.out.println(Arrays.toString(nums));
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5, 6, 7};
        int n = 3;
        //rotate(nums,n);
        rotate1(nums, n);
    }
}

